package com.example.administrator.wallet.view.activity.mine.Setting;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.text.method.TransformationMethod;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.wallet.R;
import com.example.administrator.wallet.constant.Api;
import com.example.administrator.wallet.intf.IRequestCallback;
import com.example.administrator.wallet.intf.IRequestManager;
import com.example.administrator.wallet.model.bean.AddProceedBean;
import com.example.administrator.wallet.model.bean.BindMobileVarcodeBean;
import com.example.administrator.wallet.utils.RequestFactory;
import com.example.administrator.wallet.utils.SharedPrefsUtil;
import com.example.administrator.wallet.view.activity.mine.Personal.ChangNumber.TimeButton;
import com.google.gson.Gson;

import java.lang.reflect.Field;
import java.lang.reflect.Method;


public class ForgetTradePasswordActivity extends AppCompatActivity {
    private ImageButton mIb_back,ib_first_look,ib_second_look;
    private TextView mV_title;
    private EditText et_entering_photo,et_entering_code,et_first_password,et_second_password;
    private Button bt_next;
    private TimeButton obtain;


    private boolean isHideFirst = true;// 输入框密码是否是隐藏的，默认为true
    private boolean isHideSecond = true;// 输入框密码是否是隐藏的，默认为true
    private String datas1 = "";
    private String datas2 = "";
    private String varcode = "";
    private String photo = "";

    IRequestManager reqequestManager = RequestFactory.getReqequestManager();
    private AddProceedBean addProceedBean;
    private BindMobileVarcodeBean bindMobileVarcodeBean;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_the_trade_password);

        setStatuColor();

        mIb_back = (ImageButton) findViewById(R.id.finish_img);
        mV_title = (TextView) findViewById(R.id.view_title);
        et_entering_photo = (EditText) findViewById(R.id.et_entering_photo);
        et_entering_code = (EditText) findViewById(R.id.et_entering_code);
        et_first_password = (EditText) findViewById(R.id.et_first_password);
        et_second_password = (EditText) findViewById(R.id.et_second_password);
        obtain = (TimeButton)findViewById(R.id.bt_get_code);
        bt_next = (Button) findViewById(R.id.bt_next);
        ib_first_look = (ImageButton) findViewById(R.id.ib_first_look);
        ib_second_look = (ImageButton) findViewById(R.id.ib_second_look);


        mIb_back.setVisibility(View.VISIBLE);
        mV_title.setText(R.string.Forget_the_trade_password);

        //获取验证码
        obtain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                photo = et_entering_photo.getText().toString();
                reqequestManager.httpPost(Api.BaseUrl + Api.Get_Mobile_Varcode, Api.getGetMobileVarcode(SharedPrefsUtil.getValue(ForgetTradePasswordActivity.this, "wallet", "token", null),photo), new IRequestCallback() {
                    @Override
                    public void onSuccess(String Response) {
                        Gson gson = new Gson();
                        bindMobileVarcodeBean = gson.fromJson(Response, BindMobileVarcodeBean.class);
                        if (bindMobileVarcodeBean.getCode() == 200) {
                            Toast.makeText(ForgetTradePasswordActivity.this,bindMobileVarcodeBean.getMessage(),Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(ForgetTradePasswordActivity.this,bindMobileVarcodeBean.getMessage(),Toast.LENGTH_SHORT).show();
                        }
                    }
                    @Override
                    public void onFailure(String error) {
                        Toast.makeText(ForgetTradePasswordActivity.this,error,Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
        ib_first_look.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isHideFirst == true) {
                    ib_first_look.setImageResource(R.drawable.icon_xianshixinxi);
                    //密文
                    HideReturnsTransformationMethod method1 = HideReturnsTransformationMethod.getInstance();
                    et_first_password.setTransformationMethod(method1);
                    isHideFirst = false;
                } else {
                    ib_first_look.setImageResource(R.drawable.icon_buxianshixinxi);
                    //密文
                    TransformationMethod method = PasswordTransformationMethod.getInstance();
                    et_first_password.setTransformationMethod(method);
                    isHideFirst = true;

                }
                // 光标的位置  
                int index = et_first_password.getText().toString().length();
                et_first_password.setSelection(index);
            }
        });
        ib_second_look.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isHideSecond == true) {
                    ib_second_look.setImageResource(R.drawable.icon_xianshixinxi);
                    //密文
                    HideReturnsTransformationMethod method1 = HideReturnsTransformationMethod.getInstance();
                    et_second_password.setTransformationMethod(method1);
                    isHideSecond = false;
                } else {
                    ib_second_look.setImageResource(R.drawable.icon_buxianshixinxi);
                    //密文
                    TransformationMethod method = PasswordTransformationMethod.getInstance();
                    et_second_password.setTransformationMethod(method);
                    isHideSecond = true;

                }
                // 光标的位置  
                int index = et_second_password.getText().toString().length();
                et_second_password.setSelection(index);
            }
        });
        bt_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                datas1 = et_first_password.getText().toString();
                datas2 = et_second_password.getText().toString();
                photo = et_entering_photo.getText().toString();
                varcode = et_entering_code.getText().toString();
                //设置的新密码的加密
//                    String encode = Base58.encode(datas2.getBytes());
//                    SharedPrefsUtil.putValue(SetTradePasswordActivity.this, "wallet", "tradepassword", encode);
                //上传两次密码给服务器
                reqequestManager.httpPost(Api.BaseUrl + Api.Forget_Password, Api.getForgetPassword(SharedPrefsUtil.getValue(ForgetTradePasswordActivity.this, "wallet", "token", null),photo,varcode,datas1,datas2), new IRequestCallback() {
                    @Override
                    public void onSuccess(String Response) {
                        Gson gson = new Gson();
                        addProceedBean = gson.fromJson(Response, AddProceedBean.class);
                        if (addProceedBean.getCode() == 200) {
                            Toast.makeText(ForgetTradePasswordActivity.this,addProceedBean.getMessage(),Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(ForgetTradePasswordActivity.this,addProceedBean.getMessage(),Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(String error) {
                        Toast.makeText(ForgetTradePasswordActivity.this,error,Toast.LENGTH_SHORT).show();
                    }
                });
                onBackPressed();
            }
        });
        mIb_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        startActivity(new Intent(getApplicationContext(),SettingActivity.class));
        finish();
        overridePendingTransition(R.anim.pre_in_anim, R.anim.pre_out_anim);
    }

    public void setStatuColor() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(getResources().getColor(android.R.color.white));
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        setMiuiStatusBarDarkMode(this, true);
        setMiuiStatusBarDarkMode(this, true);
    }

    //设置成白色的背景，字体颜色为黑色。
    public static boolean setMiuiStatusBarDarkMode(Activity activity, boolean darkmode) {
        Class<? extends Window> clazz = activity.getWindow().getClass();
        try {
            int darkModeFlag = 0;
            Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
            Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
            darkModeFlag = field.getInt(layoutParams);
            Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
            extraFlagField.invoke(activity.getWindow(), darkmode ? darkModeFlag : 0, darkModeFlag);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean setMeizuStatusBarDarkIcon(Activity activity, boolean dark) {
        boolean result = false;
        if (activity != null) {
            try {
                WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
                Field darkFlag = WindowManager.LayoutParams.class
                        .getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
                Field meizuFlags = WindowManager.LayoutParams.class
                        .getDeclaredField("meizuFlags");
                darkFlag.setAccessible(true);
                meizuFlags.setAccessible(true);
                int bit = darkFlag.getInt(null);
                int value = meizuFlags.getInt(lp);
                if (dark) {
                    value |= bit;
                } else {
                    value &= ~bit;
                }
                meizuFlags.setInt(lp, value);
                activity.getWindow().setAttributes(lp);
                result = true;
            } catch (Exception e) {
            }
        }
        return result;
    }
}
